<?php
/*  vim: set expandtab shiftwidth=4 softtabstop=4 tabstop=4:    */

class PDOAbstract extends PDO {
	private static $required = array('engine', 'host', 'user', 'password','name');
	
	public function __construct(array $dsn,$attr = array(PDO::ATTR_PERSISTENT => true)) {
		try {
			foreach(self::$required as $field) {
				if(!isset($dsn[$field])) {
					throw new Exception("Failed to get required defined field [".$field."]");
				}
			}
        	parent::__construct($dsn['engine'] .':host='. $dsn['host'] .';dbname='. $dsn['name'], $dsn['user'], $dsn['password'], $attr);
		} catch (PDOException $e) {
			echo "Trouble instantiating pdo [" . $e->getMessage() ."]";
		}
	}
	
	public function clean($var) {
        if(is_array($var)) return self::cleanArray($var);
        if(is_string($var)) return self::cleanString($var);
        return FALSE;
    }
    
    /*
     * Recursively cleans params as they come into methods
     */
    private function cleanArray(array $array) {
        foreach($array as $key => $a) {
            if(is_array($a)) {
                $array[$key] = self::cleanArray($a);
            } else {
                $array[$key] = self::cleanString(trim($a));
            }
        }
        return $array;
    }
    
    private function cleanString($string) {
    	if(is_string($string)) return self::quote($string);
    	return FALSE;
    }
  
	
	public function __destruct() {
		unset($this);
	}

}

?>